<?php
require_once(APP_LIB_DIR . 'MessagingModel.class.php');
include_once(APP_LIB_DIR . 'JSON.php');
#
#This is an action that checks for pending sms and send message 
# (every minute)
# 0 * * * * curl -s -o /dev/null http://localhost/MCS-SMS/?module=MCS-SMS&action=SendScheduled  
#

Class SendScheduledAction Extends Action
{
    function getDefaultView(&$controller, &$request, &$user) { return VIEW_INPUT; }

    function getRequestMethods() { return REQ_POST | REQ_GET; }

    function IsSecure() { return false; }

    function execute(&$controller, &$request, &$user)
    {
        $mm = new MessagingModel('Messaging');
        $auth = $user->getAttribute('auth');

        foreach($mm->get_scheduled() as $sched){
            $dt['message']     = $sched['message'];
            $dt['by'] = $auth['username'];

            if (!empty($sched['member_ids'])) {
                $id = $mm->insert('messages', $dt);
                foreach($mm->get_msisdn($sched['member_ids']) as $member){
                    $mm->send_group($member['msisdn'], $id,$sched['smsc'],$sched['message']);
                } 
         //       echo "contact ok";
            } elseif (!empty($sched['group_ids'])) {
                $members = $mm->group_members($sched['group_ids']);
                $id = $mm->insert('messages', $dt);
                foreach($members as $member){
                    $mm->send_group($member['msisdn'], $id,$sched['smsc'],$sched['message']);
                }
          //      echo "group ok";
            }
            $detail['status'] = 'sent';
            $mm->update('scheduler', $detail, "id = '{$sched['id']}'");
        }
        return VIEW_NONE;
    }
}
?>
